<!-- <html> -->
<head>
<title>Akka Persistence Samples in Java</title>
</head>

<body>

<div>
<h2>Akka Persistence Samples</h2>
<p>
This tutorial contains examples that illustrate a subset of
<a href="http://doc.akka.io/docs/akka/2.4-SNAPSHOT/java/persistence.html" target="_blank">Akka Persistence</a> features.
</p>
<ul>
<li>persistent actor</li>
<li>persistent actor snapshots</li>
<li>persistent actor recovery</li>
<li>persistent actor views</li>
</ul>

<p>
Custom storage locations for the journal and snapshots can be defined in
<a href="#code/src/main/resources/application.conf" class="shortcut">application.conf</a>.
</p>
</div>

<div>
<h2>Persistent actor</h2>
<p>
<a href="#code/src/main/java/sample/persistence/PersistentActorExample.java" class="shortcut">PersistentActorExample.java</a>
is described in detail in the <a href="http://doc.akka.io/docs/akka/2.4-SNAPSHOT/java/persistence.html#event-sourcing-java" target="_blank">Event sourcing</a>
section of the user documentation. With every application run, the <code>ExamplePersistentActor</code> is recovered from
events stored in previous application runs, processes new commands, stores new events and snapshots and prints the
current persistent actor state to <code>stdout</code>.
</p>

<p>
To run this example, go to the <a href="#run" class="shortcut">Run</a> tab, and run the application main class
<b><code>sample.persistence.PersistentActorExample</code></b> several times.
</p>
</div>

<div>
<h2>Persistent actor snapshots</h2>
<p>
<a href="#code/src/main/java/sample/persistence/SnapshotExample.java" class="shortcut">SnapshotExample.java</a>
demonstrates how persistent actors can take snapshots of application state and recover from previously stored snapshots.
Snapshots are offered to persistent actors at the beginning of recovery, before any messages (younger than the snapshot)
are replayed.
</p>

<p>
To run this example, go to the <a href="#run" class="shortcut">Run</a> tab, and run the application main class
<b><code>sample.persistence.SnapshotExample</code></b> several times.  With every run, the state offered by the
most recent snapshot is printed to <code>stdout</code>, followed by the updated state after sending new persistent
messages to the persistent actor.
</p>
</div>

<div>
<h2>Persistent actor recovery</h2>
<p>
<a href="#code/src/main/java/sample/persistence/PersistentActorFailureExample.java" class="shortcut">PersistentActorFailureExample.java</a>
shows how a persistent actor can throw an exception, restart and restore the state by replaying the events.
</p>

<p>
To run this example, go to the <a href="#run" class="shortcut">Run</a> tab, and run the application main class
<b><code>sample.persistence.PersistentActorFailureExample</code></b> several times.
</p>
</div>

<div>
<h2>Persistent actor views</h2>
<p>
<a href="#code/src/main/java/sample/persistence/ProcessorFailureExample.java" class="shortcut">ProcessorFailureExample.java</a>
shows how a processor can delete persistent messages from the journal if they threw an exception. Throwing an exception
restarts the processor and replays messages. In order to prevent that the message that caused the exception is replayed,
it is marked as deleted in the journal (during invocation of <code>preRestart</code>). This is a common pattern in
command-sourcing to compensate write-ahead logging of messages.
</p>

<p>
To run this example, go to the <a href="#run" class="shortcut">Run</a> tab, and run the application main class
<b><code>sample.persistence.ProcessorFailureExample</code></b> several times.
</p>

<p>
<a href="http://doc.akka.io/docs/akka/2.4-SNAPSHOT/java/persistence.html#event-sourcing" target="_blank">Event sourcing</a>
on the other hand, does not persist commands directly but rather events that have been derived from received commands
(not shown here). These events are known to be successfully applicable to current processor state i.e. there's
no need for deleting them from the journal. Event sourced processors usually have a lower throughput than command
sourced processors, as the maximum size of a write batch is limited by the number of persisted events per received
command.
</p>
</div>

<div>
<h2>Processor views</h2>
<p>
<a href="#code/src/main/java/sample/persistence/ViewExample.java" class="shortcut">ViewExample.java</a> demonstrates
how a view (<code>ExampleView</code>) is updated with the persistent message stream of a persistent actor
(<code>ExamplePersistentActor</code>). Messages sent to the persistent actor are scheduled periodically. Views also support
snapshotting to reduce recovery time.
</p>

<p>
To run this example, go to the <a href="#run" class="shortcut">Run</a> tab, and run the application main class
<b><code>sample.persistence.PersistentViewExample</code></b>.
</p>
</div>

</body>
</html>
